package cn.kgc.springday06.advice;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @author 课工场
 * @date 2023/10/11
 * @description   通知类
 */
@Component
@Aspect    //  标识当前类是通知类
public class TeacherAdviceAspect {


    // 定义公共切点表达式
//    @Pointcut("execution(String cn.kgc.springday06.impl.TeacherLi.teach*(..))")
    @Pointcut("@annotation(cn.kgc.springday06.annotatios.TeachTime)")
    public void pointcut(){}

    //  前置通知
    @Before("pointcut()")
    public void beforeAdvice(){

        System.out.println("===========上课时间到了===========");

    }


    //  后置通知   方法正确执行时执行
    @AfterReturning("pointcut()")
    public void afterReturningAdvice(){

        System.out.println("===========下课时间到了===========");

    }

    // 最终通知
//    @After("execution(String cn.kgc.springday06.impl.TeacherLi.teach*(..))")
    public void afterAdvice(){

        System.out.println("===========最终通知===========");

    }

    // 异常通知    方法抛出异常时执行
//    @AfterThrowing("execution(String cn.kgc.springday06.impl.TeacherLi.teach*(..))")
    public void afterThrowingAdvice(){

        System.out.println("===========异常通知===========");

    }

    // 环绕通知
//    @Around("execution(String cn.kgc.springday06.impl.TeacherLi.teach*(..))")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint){

        try {
            System.out.println("=========环绕前置通知============");

            Object proceed = joinPoint.proceed();  //  目标方法的执行

            System.out.println("=========环绕后置通知============");

            return  proceed;
        } catch (Throwable e) {
            System.out.println("=========环绕异常通知============");
            throw new RuntimeException(e);
        }finally {
            System.out.println("=========环绕最终通知============");
        }

    }



}
